<template>
	<view class="enter-code">
		<view class="enter-code-back"><image src="https://master.shengejiaju.cn/web/static/icons/ico_back_login.png" mode="aspectFit" @click="goback"></image></view>
		<view class="enter-code-tit">请您输入验证码</view>
		<view class="enter-code-tips">
			<view>验证码已发送到您的手机</view>
			<view>+86 {{phone}}</view>
		</view>
		<validcode ref="code" :maxlength="6" :isPwd="false" @finish="getCodeNumber" class="enter-code-box"></validcode>
		<view class="enter-code-countDown" v-if="!reSend">{{count}}s</view>
		<view class="enter-code-countDown" v-else @click="send">没收到验证码？重新发送</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				reSend: false,
				count: 60
			}
		},
		onLoad(option) {
			this.phone = option.phone
		},
		methods: {
			countDown(){
				var time = setInterval(() => {
					this.count--
					if(this.count<1){
						clearInterval(time)
						this.count = 60
						this.reSend = true
					}
				},1000)
			},
			send(){
				let data = {
					phone: this.phone,
					login_code: 'login'
				}
				this.$api.PhoneGetCode(data).then(res => {
					uni.showToast({
						icon: 'none',
						title: '发送成功',
						duration: 1500
					})
					this.reSend = false
					this.countDown()
				})
			},
			goback(){
				uni.navigateBack()
			},
			getCodeNumber(e){
				uni.hideKeyboard()
				uni.showLoading({
					mask: true
				})
				let data = {
					parent_id: '',
					//#ifdef MP-WEIXIN
					client: 1,
					// #endif
					//#ifdef APP-PLUS
					client: 2,
					// #endif
					//#ifdef H5
					client: 4,
					// #endif
					phone: this.phone,
					code: e
				}
				this.$api.PhoneLogin(data).then(res => {
					if(res.datas.role_type!=2 && res.datas.role_type!=11){
						return uni.showModal({
							title: '温馨提示',
							content: '您还不是商家管理员 请联系总部',
							showCancel: false
						})
					}
					uni.setStorageSync('userInfo',JSON.stringify(res.datas))
					uni.showToast({
						icon: 'none',
						title: '验证成功，正在登录',
						duration: 1500
					})
					setTimeout(() => {
						if(!res.datas.nickname && !res.datas.heard_img){
							uni.navigateTo({
								url: '/pages/wxbind/wxbind'
							})
						}else{
							if(uni.getStorageSync('loginUrl')){
								uni.switchTab({
									url: uni.getStorageSync('loginUrl')
								})
							}else{
								uni.switchTab({
									url: '/pages/index/index'
								})
							}
						}
					},1500)
				})
			}
		},
		mounted() {
			this.countDown()
		}
	}
</script>

<style lang="scss">
	@import '../../static/scss/login.scss'
</style>